yjx.controller('shopCartController', ['$rootScope', '$scope', '$timeout', '$fetch', 'modalService', '$init', '$locals', '$state', function ($rootScope, $scope, $timeout, $fetch, modalService, $init, $locals, $state) {
    /**购物车的逻辑 
     * 1.在初始化时获取一次 将数据放在全局作用域 购物车里面的productId不能重复
     * 2.每次进入购物车再去取 只管拿后台数据 这样的缺点在别的页面拿不到购物车的数据
    */
    $init.title("购物车");
    var api = '/shopping/goods';
    $scope.totalPrice = 0;
    $scope.cTotalPrice = 0;
    $scope.obj = {
        totalPrice: 0,
        cTotalPrice: 0,//可使用优惠券的商品总金额
    };//将price设为一个引用类型 便于子路由中price的同步更新
    $scope.scrollLeft = 80;
    $scope.parent = {
        couponList: [],
        coupons: [],
        discount: 0,
    }
    function getShopCarts() {
        $fetch.fetch(
            api,
            {
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                if (data) {
                    $scope.shopcartList = data;
                    if (data.length == 0) {
                        getRecProduct();
                    }
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    getShopCarts();
    /*获取推荐产品数据*/
    function getRecProduct() {
        $fetch.fetch(
            '/product/recommend/items',
            {
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                if (data && data.products) {
                    $scope.dataList = data.products;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }
    /**加入订单 */
    $scope.pushOrder = function (item) {
        item.isChecked = !item.isChecked;
        var price = item.price ? item.price : item.originalPrice;
        if (item.isChecked) {
            $scope.orderList.push(item);
            $scope.totalPrice = $scope.totalPrice + price;
            if(item.useCouponSate){
                $scope.cTotalPrice = $scope.cTotalPrice + price;
            }
        } else {
            $scope.isChecked = false;
            for (var i = 0; i < $scope.orderList.length; i++) {
                if (item.carItemId == $scope.orderList[i].carItemId) {
                    $scope.orderList.splice(i, 1);
                }
            }
            $scope.totalPrice = $scope.totalPrice - price;
            if(item.useCouponSate){
                $scope.cTotalPrice = $scope.cTotalPrice - price;
            }
        }
    }
    /**阻止产品跳转详情 */
    $scope.onClick = function ($event, item) {
        if ($scope.shopcartList && $scope.shopcartList.length > 0) {
            $event.preventDefault();
        }
        // pushOrder(item);
    }
    /**全选 */
    $scope.selectAll = function () {
        if (!$scope.isChecked) {
            $scope.isChecked = true;
            $scope.totalPrice = 0;
            $scope.cTotalPrice = 0;
            $scope.orderList.splice(0,$scope.shopcartList.length);
            for (var i = 0; i < $scope.shopcartList.length; i++) {
                $scope.shopcartList[i].isChecked = true;
                var price = $scope.shopcartList[i].price ? $scope.shopcartList[i].price : $scope.shopcartList[i].originalPrice
                $scope.totalPrice = $scope.totalPrice + price;
                if( $scope.shopcartList[i].useCouponSate){
                    $scope.cTotalPrice = $scope.cTotalPrice + price;
                }
                $scope.orderList.push($scope.shopcartList[i])
            }
            // $scope.orderList = $scope.shopcartList;
        } else {
            $scope.isChecked = false;
            for (var i = 0; i < $scope.shopcartList.length; i++) {
                $scope.shopcartList[i].isChecked = false;
            }
            $scope.totalPrice = 0;
            $scope.cTotalPrice = 0;
            $scope.orderList.splice(0,$scope.shopcartList.length);
        }
    }

    /**下单操作 */
    $scope.goOrder = function () {
        $scope.obj.totalPrice = $scope.totalPrice;
        $scope.obj.cTotalPrice = $scope.cTotalPrice;
        if ($scope.parent.coupons.length > 0) {
            for (var i = 0; i < $scope.parent.couponList.length; i++) {
                $scope.parent.couponList[i].isCheck = false;
            }
        }
        $scope.parent.discount = 0;
        $scope.parent.coupons = [];
        $timeout(function () { $state.go('shopcart.orderPay') })

    }
    /**删除购物车 */
    $scope.delete = function (item) {
        $scope.item = item;
        modalService.confirm("确定移除这个产品吗", confirm);
    }
    function confirm() {
        $fetch.fetch(
            '/shopping/goods/del',
            {
                params: {
                    carItemId: $scope.item.carItemId,
                    itemId: $scope.item.productId
                },
                type: 'post'
            }
        ).then(function (data) {
            $timeout(function () {
                for (var i = 0; i < $scope.shopcartList.length; i++) {
                    if ($scope.item.carItemId == $scope.shopcartList[i].carItemId) {
                        $scope.shopcartList.splice(i, 1);
                        break;
                    }
                }
                if ($scope.orderList && $scope.orderList.length > 0) {
                    for (var i = 0; i < $scope.orderList.length; i++) {
                        if ($scope.item.carItemId == $scope.orderList[i].carItemId) {
                            $scope.orderList.splice(i, 1);
                            var price = $scope.item.price ? $scope.item.price : $scope.item.originalPrice;
                            $timeout(function () {
                                $scope.totalPrice = $scope.totalPrice - price;
                                if( $scope.item.useCouponSate){
                                    $scope.cTotalPrice = $scope.cTotalPrice - price;
                                }
                            })

                            break;
                        }
                    }
                }
                /**购物车内容删没了 */
                if ($scope.shopcartList.length == 0) {
                    getRecProduct();
                }
            })

        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }

    $scope.orderList = [];
}])